/**
 * 
 */
package exteditapp.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

import exteditapp.IModuleApplet;
import exteditapp.ModuleAppletDefault;

/**
 * Default interface for button actions.
 * @author Arturas Smorgun, arturas@smorgun.lt
 * @version 2009.04.27
 */
public abstract class IAction extends JMenuItem implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8166735535166287515L;

	/**
	 * Link to main application module.
	 */
	protected IModuleApplet main = null;
	
	/**
	 * List of menu actions. If UI in button mode - first one becomes main, another goes to menu,
	 * in progress mode all goes to menu. By default consist only of action itself.
	 */
	protected IAction[] actionList = {
		this,
	};
	
	protected String[] myTextArray;
	
	/**
	 * Just example of menu initialization
	 */
	public IAction(){
		this.addActionListener(this);
		this.main = ModuleAppletDefault.getInstance();
	};
	public IAction(boolean initMenu){
		this.addActionListener(this);
		this.main = ModuleAppletDefault.getInstance();
		if(!initMenu)
			return;
		this.actionList = new IAction[] {
		};
	}
	
	/**
	 * Action initialization
	 */
	protected void init(){
	}
	
	/**
	 * Action initialization
	 * @param isEnabled enables or disables button/element
	 */
	protected void init(boolean isEnabled){
		this.setEnabled(isEnabled);
	}
	
	/**
	 * Action initialization
	 * @param isEnabled enables or disables button/element
	 * @param newLabel sets new label
	 */
	protected void init(boolean isEnabled, String newLabel){
		this.setEnabled(isEnabled);
		this.setText(newLabel);
	}
	
	public void actionPerformed(ActionEvent e){
		this.execute();
	}
	
	/**
	 * Abstraction of action execution (what happens then button pressed or element selected)
	 */
	public abstract void execute();
	
	/**
	 * Action execution of Nth element in a list (what happens then button pressed or element selected)
	 * @param n index of element in a list
	 */
	public void execute(int n){
		if(this.actionList.length > n && this.actionList[n] != null){
			this.actionList[n].execute();
		}
	}
	
	/**
	 * Returns current label value
	 * @return label value
	 */
	public String getLabel(){
		return this.getText();
	}
	
	/**
	 * Returns label of Nth element in a list
	 * @param n index of element in a list
	 * @return label of Nth element in a list
	 */
	public String getText(int n){
		if(this.actionList.length > n && this.actionList[n] != null)
			return this.actionList[n].getText();
		return "";
	}
	
	public void updateText(int sec){
		if(myTextArray != null){
			if(myTextArray.length > 2){
				switch(sec % 10){
					case 4:
					case 5:
					case 6:
						this.setText(this.myTextArray[1]);
						break;
					case 7:
					case 8:
					case 9:
						this.setText(this.myTextArray[2]);
						break;
					default:
						this.setText(this.myTextArray[0]);
				}
			}
			else if(myTextArray.length > 1){
				switch(sec % 10){
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
						this.setText(this.myTextArray[1]);
						break;
					default:
						this.setText(this.myTextArray[0]);
				}
			}
		}
	}
	
	/**
	 * Sets current label value
	 * @param newLabel new label value
	 */
	protected IAction setTextGetAction(String newLabel){
		this.setText(newLabel);
		return this;
	}
	
	public void setText(String label){
		if(label.indexOf('#') >= 0){
			this.myTextArray = label.split("#");
			super.setText(this.myTextArray[0]);
		}
		else
			super.setText(label);
	}
	
	/**
	 * Sets label of Nth element in a list
	 * @param n index of element in a list
	 * @param newLabel new label value
	 */
	public void setText(int n, String newLabel){
		if(this.actionList.length > n && this.actionList[n] != null)
			this.actionList[n].setText(newLabel);
	}
	
	public void setEnabled(int n, boolean isEnabled){
		if(this.actionList.length > n && this.actionList[n] != null)
			this.actionList[n].setEnabled(isEnabled);
	}
	
	public int getItemCount(){
		return this.actionList.length;
	}
	
	public IAction getItem(int n){
		if(this.actionList.length > n && this.actionList[n] != null)
			return this.actionList[n];
		return null;
	}
	
}
